Note : Always profile and benchmark your code first.
-
Always prefer use builtin functions, because most of the time they are way too faster as being implemented in C. (for cpython)
-
Use sets instead of lists, as it provides much faster lookups.
-
Code snippet :
for i in range(100):
doSomethingWithX() # Much slower
map(doSomethingWithX() , xrange(0,100)) # Much Faster , because interpreter only have to resolve the function name once.- try'ing is cheap, if'ing is expensive , when the chances of getting into the
exceptionare less (execution insideexceptblock is slower).
# Slower
if (type(a) == str):
a = int(a)
# Faster : python eafp https:#stackoverflow.com/questions/11360858/what-is-the-eafp-principle-in-python
try:
a = int(a)
except:
pass- For generating a long string ( 'str_generated' ) in python :
# Slower
str_generated = ""
for x in list:
str_generated += some_function(x)
# Faster
slist = [some_function(x) for x in somelist]
str_generated = "".join( slist )
Reason : python strings are immutable so everytime you do '+=' it everytime creates new string.-
Use generators over iterators.(It is memory efficient, but doesn't effect the speed of execution)
-
Global import statements are faster than local import statements ( import written inside a function), this is the behaviour of python interpreter.
-
Function call overhead in Python is relatively high, (especially compared with the execution speed of a builtin function), This strongly suggests that wherever appropriate, functions should handle data aggregates.
-
How to find double of a number (say 'num') ?
a. num = num + num
b. num = num*2
c. num = num<<2
Expected performance : a (worst) < b < c (Best, as it's a bit shift)
Observed performance : a (Best)
The reason we expect 'c' to be best is because the compiler(in C++,java etc) converts it into 1 machine instruction, whereas python doesn't have that concept.-
For infinite loop
while 1is better thanwhile Truethe reason being thatwhile 1is converted into a single jump operation , this is inpython2only , forpython3it's the same. The reason for this being inpython2Trueis not listed as a keyword, so everytime the interpreter has to fetch it's value and the evaluate whereas inpython3Trueis listed as a keyword and thus it doesn't have to fetch everytime. -
List comprehensions are better than
forloop -
Avoid global variables as much as possible (not a good pratice too) , python lookup in global space is too time consuming as compared to local variables.
-
Avoid unwanted loops (shouldn't be saying this though)
For finding common element in 2 lists 'a' and 'b' :
for x in a:
for y in b:
if x == y:
c.append(x)
return c
return set(a) & set(b) # Obv this is faster-
Keep your python code small and tight, don't forget python is interpreted after-all.
-
Use numpy incase of arrays mathematical computation etc. after all it's in cython and thus much faster. For more speed port to cython.